<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .wrapper {
            min-width: 400px;
            max-width: 800px;
            display: flex;
            flex-direction: row;
        }

        .avatar {
            width: 48px;
            height: 48px;
            background-color: rebeccapurple;
            overflow: hidden;
        }

        .wrapper textarea {
            outline: none;
            border-color: aqua;
            border-radius: 4px;
            flex: 1;
            padding: 10px;
            height: 30px;
            transition: all .5s;
        }

        .wrapper textarea:focus {
            background-color: blue;
            height: 35px;
            background: #fff;
        }

        .wrapper button {
            width: 70px;
            border: none;
            cursor: pointer;
            background-color: violet;
        }

        .wrapper .total {
            margin-left: 80%;
            margin-top: 5px;
            opacity: 0;
            transition: all 1s;
        }

        .list {
            min-width: 400px;
            max-width: 800px;
            display: flex;
        }

        .list .item {
            display: flex;
            width: 100%;
        }

        .list .item .info {
            flex: 1;
            padding-bottom: 10px;
        }

        .list .item p {
            margin: 0;
        }

        .list .item .name {
            font-weight: bold;
            font-size: 15px;
        }

        .list .item .text {
            color: #333;
            padding: 10px 0;
        }

        .list .item .time {
            color: #999;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <i class="avatar"></i>
        <textarea name="" id="tx" placeholder="发布一条友善的评论" rows="2"></textarea>
        <button>发布</button>
    </div>
    <div class="wrapper">
        <span class="total">0/200字</span>
    </div>
    <div class="list">
        <div class="item" style="display: none;">
            <i class="avatar"></i>
            <div class="info">
                <p class="name">Basil</p>
                <p class="text">你好</p>
                <p class="time">2024-5-14 16:47</p>
            </div>
        </div>
    </div>

    <script>
        const tx = document.querySelector('#tx')
        const total = document.querySelector('.total')
        const item = document.querySelector('.item')
        const text = document.querySelector('.text')
        const btn = document.querySelector('button')

        tx.addEventListener('focus', function () {
            total.style.opacity = 1
        })
        tx.addEventListener('blur', function () {
            total.style.opacity = 0
        })
        tx.addEventListener('input', function () {
            total.innerHTML = `${tx.value.length}/200字`
        })

        function fn() {
            if (tx.value.trim() !== '') {
                item.style.display = 'block'
                text.innerHTML = tx.value
            }
            tx.value = ''
            total.innerHTML = '0/200字'
        }

        tx.addEventListener('keyup', function (e) {
            if (e.key === 'Enter') {
                fn()
            }
        })
        btn.addEventListener('click', function () {
            fn()
        })
    </script>
</body>

</html>